<?php  


    session_start();


    //error_reporting(E_ALL);

    //connect to the database 
    $mysqli = new mysqli("127.0.0.1", "mishoppa", "lk^36yfgh23GF", "ailerons");

    // check connection
    if ($mysqli->connect_errno) {
        printf("Connect failed: %s\n", $mysqli->connect_error);
        exit();
    }

       
    // Create table
    /*if ($mysqli->query("CREATE TABLE frequencies (    id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,    airport_ident CHAR(7),    type varchar(20),    description varchar(255),    frequency_mhz varchar(10) );"       ) === TRUE) {
        printf("Table frequencies successfully created.\n");
    } else {
        echo $mysqli->error;
    }*/

    // reset table
    /*if ($mysqli->query("drop table runways;") === TRUE) {
        printf("aerodromes reset.\n");
    }*/


    // count rows
    if ($result = $mysqli->query("SELECT * FROM frequencies;")) {
        //printf("Select returned %d rows.\n", $result->num_rows);
        echo $result->num_rows;
        
        $result->close();
    }



if (isset($_SESSION['token']) && $_POST['token'] == $_SESSION['token']) {

    if ($_FILES[csv][size] > 0) { 

        //get the csv file 
        $file = $_FILES[csv][tmp_name]; 
        $handle = fopen($file,"r"); 
         
        //loop through the csv file and insert into database 

        //$table = 'aerodromes';
        //$fields = array("ident","type","name","latitude_deg","longitude_deg","elevation_ft","continent","iso_country","iso_region","municipality");
        //$table = 'runways';
        //$fields = array("airport_ident",    "length_ft",    "width_ft",    "surface", "lighted",    "le_ident",    "le_latitude_deg",    "le_longitude_deg",    "le_elevation_ft",    "le_heading_degT",    "le_displaced_threshold_ft",    "he_ident",    "he_latitude_deg",    "he_longitude_deg",    "he_elevation_ft",   "he_heading_degT",    "he_displaced_threshold_ft");        
        //$table = 'frequencies';
        //$fields = array("airport_ident","type","description","frequency_mhz");
        $x = 0;
        
        do { 
            if ($data[0]) { 
                $query = "INSERT INTO " . $table . " VALUES (NULL,";
                foreach ($fields as $key => $field) {
                    $query .= "'" . $data[$key];
                    $query .= ($key==count($data)-1) ? "'" : "',";                        
                }

                $query .= ");";
                //$query = $mysqli->real_escape_string($query);

                //echo $query . "<br>";
                if ($mysqli->query($query) === TRUE) {
                    //printf($query . "\n");
                } else {
                    printf("[%d] %s\n", $mysqli->errno, $mysqli->error);
                    echo "<br>" . $query . "<br>";
                    //echo $mysqli->error . "<br>";
                }

            } 

            $x++;

            //if ($x > 10) break;

        } while ($data = fgetcsv($handle,5000,",","'")); 

    }


    $mysqli->close();
}

$token = md5(uniqid(rand(), true));
$_SESSION['token'] = $token;


?> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> 
<title>Import a CSV File with PHP & MySQL</title> 
</head> 

<body> 

<?php if (!empty($_GET[success])) { echo "<b>Your file has been imported.</b><br><br>"; } //generic success notice ?> 

<!--<form action="" method="post" enctype="multipart/form-data" name="form1" id="form1"> 
  Choose your file: <br /> 
  <input name="csv" type="file" id="csv" /> 
  <input type="submit" name="Submit" value="Submit" /> 
  <input type="hidden" name="token" value="<?php echo $token; ?>" />
</form>-->

</body> 
</html> 